package com.xiaoyu.lanling.feature.guard.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.xiaoyu.base.model.User
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.BaseFragment
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.guard.CloseEvent
import com.xiaoyu.lanling.feature.gift.ChatGiftBottomSheetDialog
import kotlinx.android.synthetic.main.guard_detail_other_nobody_fragment.*

class GuardDetailOtherNobodyFragment : BaseFragment() {

    companion object {
        fun newInstance(guardee: User, guarder: User, from: String, coinNum: Int): GuardDetailOtherNobodyFragment {
            val arguments = Bundle()
            arguments.putSerializable(Constant.IntentKey.KEY_GUAREE, guardee)
            arguments.putSerializable(Constant.IntentKey.KEY_GUARER, guarder)
            arguments.putInt(Constant.IntentKey.KEY_COIN_NUM, coinNum)
            arguments.putString(Constant.PARAM_FROM, from)

            val fragment = GuardDetailOtherNobodyFragment()
            fragment.arguments = arguments
            return fragment
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.guard_detail_other_nobody_fragment, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)

        initView()
        initBind()
    }

    private fun initView() {
        val coinNum = arguments?.getInt(Constant.IntentKey.KEY_COIN_NUM) ?: return
        tip.text = getString(R.string.guard_detail_other_nobody_tip, coinNum.toString())
    }

    private fun initBind() {
        guard_button?.setOnClickDebounceListener {
            val guardee = (arguments?.getSerializable(Constant.IntentKey.KEY_GUAREE) as? User)
                    ?: return@setOnClickDebounceListener
            val from = arguments?.getString(Constant.PARAM_FROM) ?: ""
            ChatGiftBottomSheetDialog.show(childFragmentManager, guardee, from)
        }
        leave_button?.setOnClickDebounceListener { CloseEvent().post() }
    }
}